Skip to content

feat(agent): turn-level retry with idle timeout, plus withToolRetry helper#52

Closed
robert-j-y wants to merge 2 commits into
mainfrom
robert/turn-level-retry
Closed

feat(agent): turn-level retry with idle timeout, plus withToolRetry helper#52
robert-j-y wants to merge 2 commits into
mainfrom
robert/turn-level-retry

Conversation

@robert-j-y

Copy link
Copy Markdown
Collaborator

Summary

Fusion benchmark failures traced to mid-loop stream deaths: a turn's SSE stream opens fine, emits ~0–5 tokens, then either ends with no terminal event (Follow-up stream ended without a completed response) or hangs silently forever. Today the whole callModel promise rejects and callers can only restart from turn 0 — re-running every tool call and losing all gathered context.

  • retryTurn option on callModel{ limit, isRetryable, backoffMs, idleTimeoutMs }. Wraps each turn's send+consume (initial, follow-up, and final-response turns) and re-sends just the failed turn's request with the accumulated conversation state intact. Typed errors (TurnStreamEndedError, TurnResponseFailedError, TurnIdleTimeoutError) drive the default retryable classification (5xx/transport/stream-death yes; 4xx account-state no).
  • idleTimeoutMs — converts silent stream hangs (stream never closes, nothing throws) into retryable turn failures instead of relying on an external inactivity guard killing the request.
  • turn.retry event — emitted on the response stream before each retry attempt so consumers can observe retries.
  • withToolRetry helper — separately wraps a tool's execute with bounded retries so transient tool throws are absorbed instead of being serialized as {"error": ...} results that burn a model turn (per Matt's suggestion).

Includes changeset for a minor release (0.8.0). Note: branch builds on the two unpushed atomicity commits (ce00bf9, 7b6704a).

Test plan

  • New unit tests: tests/unit/turn-retry.test.ts (follow-up/initial turn retries, idle timeout, retry limits, custom isRetryable, backoff, turn.retry event semantics)
  • New unit tests: tests/unit/tool-retry.test.ts (regular + generator tools, limit exhaustion, isRetryable, onRetry, type preservation)
  • Full unit suite, typecheck, lint green
  • Wire into fusion inner calls in openrouter-web after 0.8.0 publish (prepared behind a TODO at the callModel site)

Made with Cursor

robert-j-y and others added 2 commits June 11, 2026 12:53
…elper

When a turn's response stream dies mid-loop (ends without a terminal event, fails, or hangs silently), the whole callModel promise rejected and callers could only restart from turn 0, re-running every tool call. retryTurn re-sends just the failed turn's request with the accumulated conversation state intact, and idleTimeoutMs converts silent stream hangs into retryable failures. Retries surface as turn.retry events. withToolRetry separately absorbs transient tool execute throws so they don't burn a model turn.

Co-authored-by: Cursor <cursoragent@cursor.com>
@robert-j-y

Copy link
Copy Markdown
Collaborator Author

Parking this for now per team discussion — we'll handle recovery at the application layer (fusion/server-tools in openrouter-web) until the comments-based stream-liveness approach is designed for the SDK. Branch stays up for reference.

@robert-j-y robert-j-y closed this Jun 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant